package leetcode.code0659;

import java.util.PriorityQueue;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//执行用时：11 ms, 在所有 Java 提交中击败了95.05%的用户
//
//内存消耗：41.9 MB, 在所有 Java 提交中击败了9.41%的用户
//
//通过测试用例：186 / 186
public class Solution implements IDebug {

	public boolean isPossible(int[] nums) {
		PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
		for (int num : nums) {
			if (!pq.isEmpty() && pq.peek()[0] != num) {
				while (!pq.isEmpty() && pq.peek()[0] != num - 1) {
					if (pq.peek()[1] >= 3) {
						pq.poll();
					} else {
						return false;
					}
				}
				if (!pq.isEmpty()) {
					int[] cur = pq.poll();
					pq.add(new int[] { num, cur[1] + 1 });
				} else {
					pq.add(new int[] { num, 1 });
				}
			} else {
				pq.add(new int[] { num, 1 });
			}
		}
		while (!pq.isEmpty()) {
			int[] cur = pq.poll();
			if (cur[1] < 3) {
				return false;
			}
		}
		return true;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(true, this.isPossible(HelpDebug.str2array3("[1,2,3,3,4,4,5,5]")));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(true, this.isPossible(HelpDebug.str2array3("[1,2,3,3,4,5]")));

	}

	@Override
	public void debug3() {
		HelpDebug.compare(false, this.isPossible(HelpDebug.str2array3("[1,2,3,4,4,5]")));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
